package androidvn.xenforo.fourums.adapter;

import java.util.ArrayList;

import androidvn.xenforo.fourums.R;
import androidvn.xenforo.fourums.model.Thread;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomThreadListAdapter extends BaseAdapter {
	Context context;
	ArrayList<Thread> threadList;

	public CustomThreadListAdapter(Context context, ArrayList<Thread> threadList) {
		super();
		this.context = context;
		this.threadList = threadList;
	}

	@Override
	public int getCount() {
		return threadList.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup viewGroup) {
		ViewHolder holder = null;

		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.thread_list_item,
					viewGroup, false);

			holder = new ViewHolder();
			holder.ivAvatar = (ImageView) convertView
					.findViewById(R.id.ivAvatar);
			holder.tvUsername = (TextView) convertView
					.findViewById(R.id.tvUsername);
			holder.tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
			holder.tvViewCount = (TextView) convertView
					.findViewById(R.id.tvViewCount);
			holder.tvReplyCount = (TextView) convertView
					.findViewById(R.id.tvReplyCount);
			holder.tvPostDate = (TextView) convertView
					.findViewById(R.id.tvPostDate);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		// holder.ivAvatar
		holder.tvUsername.setText(threadList.get(position).getUserName());
		holder.tvTitle.setText(threadList.get(position).getTitle());
		holder.tvViewCount.setText(threadList.get(position).getViewCount());
		holder.tvReplyCount.setText(threadList.get(position).getReplyCount());
		holder.tvPostDate.setText(threadList.get(position).getPostDate());
		return convertView;
	}

	static class ViewHolder {
		TextView tvUsername, tvTitle, tvViewCount, tvReplyCount, tvPostDate;
		ImageView ivAvatar;
	}
}
